# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=suitesparse
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-suitesparse")
pkgver=5.10.1
pkgrel=1
pkgdesc='A suite of sparse matrix algorithms (mingw-w64)'
url="https://faculty.cse.tamu.edu/davis/suitesparse.html"
license=('GPL')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/DrTimothyAldenDavis/SuiteSparse/archive/v${pkgver}.tar.gz"
        "0001-mingw-w64-Use-a-not-lib-as-AR_TARGET-extension.patch"
        "0002-mingw-w64-Set-SO_OPTS--shared-move-dlls-create-import-libs.patch"
        "0003-mingw-w64-no-CUDA.patch"
        "0004-mingw-w64-install-static-libs.patch"
        "0005-suitesparse-5.8.1-skip-building-Mongoose-GraphBLAS.patch"
        "0006-suitesparse-5.8.1-fix-mp-link-order.patch")
depends=($([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && \
           echo "${MINGW_PACKAGE_PREFIX}-openblas" ||
           echo "${MINGW_PACKAGE_PREFIX}-f2cblaslapack")
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-metis")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs')
sha256sums=('acb4d1045f48a237e70294b950153e48dce5b5f9ca8190e86c2b8c54ce00a7ee'
            '1c848ba4fd5b07d65e0f8e9f6d5153efc51292d0d487d446afbdacf25e4fd8f7'
            '537f4fcf973ee6b38437d3ccb416fe851d709edb4b84f730169dd48ac7175000'
            '8a94c54322323773442c2c60f54795d402d64ac126e2a1a67e48a0075f8571fa'
            'e0faab259d5c474c6af51c9b471f645810a308a1c83976275b1969e81e7d8390'
            'a63a1d91bb31f50010c48d112696212deafd1379cc494e711ab9e53724085d19'
            '1cf6cd948e5178cbcb16bb0d3c97829311250dedda1e9dcbe0c85dd6bdc4461e')

prepare() {
  cd "${srcdir}"/SuiteSparse-${pkgver}
  patch -p1 -i "${srcdir}"/0001-mingw-w64-Use-a-not-lib-as-AR_TARGET-extension.patch
  patch -p1 -i "${srcdir}"/0002-mingw-w64-Set-SO_OPTS--shared-move-dlls-create-import-libs.patch
  patch -p1 -i "${srcdir}"/0003-mingw-w64-no-CUDA.patch
  patch -p1 -i "${srcdir}"/0004-mingw-w64-install-static-libs.patch
  patch -p1 -i "${srcdir}"/0005-suitesparse-5.8.1-skip-building-Mongoose-GraphBLAS.patch
  patch -p1 -i "${srcdir}"/0006-suitesparse-5.8.1-fix-mp-link-order.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -rf "${srcdir}/SuiteSparse-${pkgver}" "${srcdir}/build-${MSYSTEM}"

  cd "${srcdir}/build-${MSYSTEM}"

  # This avoids needing to use a very large patch (we still get diffs but
  # that is down to a bug in format string handling somewhere where-by an
  # extra 0-padding value is emitted in exponents of scientific floats).
  [[ -d "${PWD}/bin" ]] || mkdir "${PWD}/bin"
  echo "#!/usr/bin/env bash"                         > "${PWD}/bin/diff"
  echo "/usr/bin/diff --strip-trailing-cr \"\$@\""  >> "${PWD}/bin/diff"

  # This avoids needing to use another very large patch.
  [[ -d "${PWD}/lib" ]] || mkdir "${PWD}/lib"
  ar cru "${PWD}/lib/librt.a"

  PATH="${PWD}/bin:${PATH}"          \
  CC=${CC}                           \
  CXX=${CXX}                         \
  CFLAGS="-DLONGBLAS='long long'"    \
  CXXFLAGS="-DLONGBLAS='long long'"  \
  LDFLAGS="-L${PWD}/lib"             \
  MY_METIS_LIB=-lmetis               \
  BLAS=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo -lopenblas || echo -lblas) \
  LAPACK=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo -lopenblas || echo -llapack) \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    make CMAKE_OPTIONS="-G\"MSYS Makefiles\" -DCMAKE_INSTALL_PREFIX=\"${MINGW_PREFIX}\" -DCMAKE_BUILD_TYPE=Release"
}

package() {
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/{bin,lib,include} || true

  cd "${srcdir}/build-${MSYSTEM}"

  PATH=${PWD}/bin:"${PATH}"               \
  CC=${CC}                                \
  CXX=${CXX}                              \
  CFLAGS="-DLONGBLAS='long long'"         \
  CXXFLAGS="-DLONGBLAS='long long'"       \
  MY_METIS_LIB=-lmetis                    \
  BLAS=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo -lopenblas || echo -lblas) \
  LAPACK=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo -lopenblas || echo -llapack) \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  make install                          \
    INSTALL="${pkgdir}${MINGW_PREFIX}"  \
    DESTDIR="${pkgdir}"                 \
    CMAKE_OPTIONS="-G\"MSYS Makefiles\" -DCMAKE_INSTALL_PREFIX=\"${MINGW_PREFIX}\" -DCMAKE_BUILD_TYPE=Release"
}
